package com.crawler.service

import com.crawler.db.{CrawlerTaskRepository, H2CrawlerTaskRepository}
import com.crawler.message.TaskParamMessage
import com.crawler.tables.Tables.TaskRow
import com.crawler.utils.MD5Utils
import org.slf4j.LoggerFactory

import scala.collection.mutable.ListBuffer

/**
  * Created by tongtao.zhu on 2016/11/23.
  * 负责将task存储到数据库
  */
class DBService {
  val log = LoggerFactory.getLogger(DBService.getClass)
  var crawlerTaskRepository: CrawlerTaskRepository = _

  def create(crawlerTask: TaskRow): Boolean = {
    //在插入之前先查找是否已经存在于表中
    val tasks = crawlerTaskRepository.find(crawlerTask)
    if (tasks.length > 0){
      return false
    }
    crawlerTaskRepository.insert(crawlerTask)
    true
  }

  def createBatch(tasks: List[TaskParamMessage], dbName: String): Unit ={
    crawlerTaskRepository.bulkInsert(tasks, dbName)
  }

  def find(md5Tasks: List[String], dbName: String): List[TaskParamMessage] = {
    val tasksFind = crawlerTaskRepository.findByMd5(md5Tasks, dbName: String)
    tasksFind
  }

  def findTasks(dbName: String): List[TaskParamMessage] = {
    val listTasks = new ListBuffer[TaskParamMessage]
    val initTasks = crawlerTaskRepository.findByPrior(dbName)
    val failTasks = crawlerTaskRepository.findTimeOutTask(dbName)
    listTasks ++= initTasks
    listTasks ++= failTasks
    listTasks.toList
  }

  def findRunningTask(dbName: String): Int = {
    crawlerTaskRepository.findRunningTasks(dbName)
  }

  def findTaskGroup(dbName: String): List[Tuple2[Int, Long]] = {
    crawlerTaskRepository.findTasksGroup(dbName)
  }

  def update(crawlerTask: TaskRow) = {
    crawlerTaskRepository.update(crawlerTask)
  }

  def batchUpdate(crawlerTasks: List[TaskParamMessage]): Unit ={
    crawlerTaskRepository.batchUpdate(crawlerTasks)
  }

  def createOrUpdate(tasks: List[TaskParamMessage]): Unit ={
    if (tasks.size==0){
      return
    }
    val createTasks = tasks.filter(_.status == 0).toSet
    val updateTasks = tasks.filter(_.status == 1)

    if (updateTasks.size > 0){
      log.info(s"update ${updateTasks.size} task")
      this.batchUpdate(updateTasks)
    }

    if (createTasks.size > 0){
      val mD5List = createTasks.map(task => MD5Utils.getMd5String(task)).toList
      val ta = this.find(mD5List, createTasks.toList(0).source).toSet
      val taMd5List = ta.map(task => MD5Utils.getMd5String(task))
      val createTasksDistinct = createTasks.filter(task =>
        if (taMd5List.contains(MD5Utils.getMd5String(task))){
          false
        }else{
          true
        }
      )
      if (createTasksDistinct.size > 0){
//        log.info(s"已经存在的个数为：${ta.size},总数为：${createTasks.size},需要插入的个数为:${createTasksDistinct.size}")
        this.createBatch(createTasksDistinct.toList, createTasksDistinct.head.source)
      }
    }
  }
}

object DBService{
  val dbService = new DBService()
  val h2 = new H2CrawlerTaskRepository()
  dbService.crawlerTaskRepository = h2
}
